package com.vm.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.vm.adapter.AddressManageAdapter;
import com.vm.adapter.AddressSelectAdapter;
import com.vm.commons.CurrentUser;
import com.vm.commons.VMConst;
import com.vm.model.UserAddress;
import com.vm.utils.JsonUtils;
import com.wwt.app.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月18日 下午1:36:35]
 * @Version: [v1.0]
 * 
 */
public class AddressManageActivity extends Activity {

	protected static final String TAG = "AddressManageActivity";
	Button btn_add_address;
	String userId;
	List<UserAddress> list;
	RequestQueue mQueue;
	ImageView imgbtn_search_back;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.address_manage);

		init();

		mQueue = Volley.newRequestQueue(this);

		list = new ArrayList<UserAddress>();
		String url = VMConst.REQUEST_URL + "getAllAddress?userId="
				+ CurrentUser.USERID;
		JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(url,
				new Listener<JSONArray>() {
					@Override
					public void onResponse(JSONArray arg0) {
						// arg0.length();

						for (int i = 0; i < arg0.length(); i++) {
							try {
								JSONObject jsonObject = arg0.getJSONObject(i);
								UserAddress userAddress = JsonUtils.jsonToBean(
										jsonObject, UserAddress.class);
								list.add(userAddress);

							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}

						try {
							AddressManageAdapter myAdapter = new AddressManageAdapter(
									AddressManageActivity.this, list);
							ListView listView = (ListView) findViewById(R.id.lv_address);

							listView.setAdapter(myAdapter);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							Log.e(TAG, e.getMessage());
						}
						//
						// Log.i(TAG, Integer.toString(listsofts.size()));
						// // arg0.getJSONObject(index);

					}

				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError error) {
						Log.e(TAG, error.getMessage(), error);
					}
				});

		mQueue.add(jsonObjectRequest);

	}

	/**
	 * 初始化
	 */
	public void init() {
		btn_add_address = (Button) findViewById(R.id.btn_add_address);
		btn_add_address.setOnClickListener(btn_ClickListener);
		
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
	}

	OnClickListener btn_ClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(AddressManageActivity.this,
					AddressAddActivity.class);
			startActivity(intent);

		}
	};
	
	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}

}
